<?php

    class File_Folders extends ActiveRecord {
        
        function _setup()
        {
            $this->table = DB_PREFIX.'file_folders';
        }        
        
        function validate()
        {
            $this->validates_format_of('name',"!^[\w\d]+$!",'Folder name can not contain special characters or spaces');
            
            // checks to see if folder already exists
            $fcheck = $this->db->GetOne('SELECT name FROM `'.$this->table.'` WHERE name=? AND parent_id=?',
                                        array($this->get('name'),$this->get('parent_id')));
            if ( $fcheck )
            {
                $this->add_error('name','Folder already exists. Please choose a different name');
            }
        }
        
        function after_validation_on_create()
        {
            // make dir
            $path = $this->get_server_path($this->get('parent_id'));
            mkdirs($path.'/'.$this->get('name'));
        }
        
        function get_server_path($parent_id)
        {
            $path = $this->recurse_up($parent_id);
            $new  = array_reverse($path);
            return PUBLIC_HTML.'/'.implode('/',$new);
        }
        
        function get_public_path($parent_id)
        {
            $path = $this->recurse_up($parent_id);
            $new  = array_reverse($path);        
            return implode('/',$new);
        }
        
        function get_breadcrumb($folder_id)
        {
            $path = $this->recurse_up($folder_id);
            return array_reverse($path,true);
        }
        
        function recurse_up($parent_id,$path=array())
        {           
           $folder = $this->find_rows(array(
                                         'search'=>'first',
                                         'conditions'=> array('id=?',$parent_id),
                                         'fields'=>array('name','id','parent_id')
                                       )                                      
                                 );
           if($folder){                                                     
               $path[$folder['id']] = $folder['name'];
               if($folder['parent_id'] != 0){
                   $path = $this->recurse_up($folder['parent_id'],$path);
               }
           }
           return $path;
       }
        
        function delete_folder($id)
        {
            $this->load($id);
            if ( $this->is_loaded() )
            {
                $path = $this->get_server_path($id);
                if ( $this->delete_from_server($path) ) {
                    $this->delete_from_db($id);
                }
            } else {
                trigger_error('Can not delete folder, no id loaded',WARNING);
            }
            return false;
        }
        
        // this function recursively deletes files and folders from server
        function delete_from_server($target)
        {
            $sourcedir = opendir($target); 
            // delete all file children  
            while(false !== ($filename = readdir($sourcedir)))
            {           
                if($filename != '.' && $filename != '..'){
                    if(is_dir($target."/".$filename)){
                        $this->delete_from_server($target."/".$filename);
                    } else if(is_file($target."/".$filename)){
                       unlink($target."/".$filename);
                    }
                }
            }
            closedir($sourcedir);
            // delete target dir
            if(rmdir($target)){ 
                return true; 
            }else{ 
                return false; 
            }
        } 
        
        // this function recursively deletes files and folders from db
        function delete_from_db($id)      
        {
            $folders = $this->find_rows(
                                    array(
                                        'conditions'=>array('parent_id=?',$id),
                                        'fields'=>'id'
                                        )
                                  );        
            // delete folder and its files
            $this->db->Execute('DELETE FROM `'.$this->table.'` WHERE id=?',array($id));
            $this->db->Execute('DELETE FROM `'.DB_PREFIX.'files` WHERE file_folder_id=?',array($id));    

           // delete children files
           if($folders){
               foreach($folders as $folder){
                   $this->delete_from_db($folder['id']);    
               }
           }
        }
        
        function get_sub_folders($id)
        {
            return $this->find_rows( array('conditions'=>array('parent_id=?',$id), 'order'=>'name') );
        }
    }

?>